
import controlP5.*;


Cargador car;
Slider slider;
Helper helper;
ControlP5 controlP5;
Explorador explorador;
boolean cargado=false;
int estado=0;
int x1=0;
int y1=0;
int etiqueta=-1;
int sliderValue=0;
ArrayList puntos= new ArrayList();
PImage b;
ControlGroup mBox;
ListBox l;
boolean msg=false;
boolean elim=false;
boolean modif=false;
boolean creando=false;
boolean mpress = true;

int resAncho=-1;
int resAlto=-1;
int resRadio=-1;
int nEt = 0;
float imgAncho=0;
float imgAlto=0;
float imgAncho2=0;
float imgAlto2=0;
private float zoom=1;
private float dragX=0;
private float dragY=0;
private float xd=0;
private float yd=0;
private float zm =0;
private float zoomy=1;
private PFont font;

private final String MSGIDLE = "Controls: MOUSE LEFT - Select  MOUSE RIGHT - Drag  MOUSE WEHEEL - Zoom";


Herramienta herActual;

PImage fondoEliminar;
PImage marco;
PImage opciones;

controlP5.Button eliminar,modificar;


void setup() {
  size(1024,768);

  font = loadFont("ComicSansMS-Bold-48.vlw");
  
  
  b= loadImage("fondo.png");
 
  


  slider = new Slider();
  explorador = new Explorador(slider.getAlto());
  //explorador.cargarConf(this);
  helper = new Helper(250,explorador.getAncho(),slider.getAlto());
  helper.setVisible(false);
  helper.setText(MSGIDLE);
  controlP5 = new ControlP5(this);
  
  
  
  controlP5.addButton("Load", 0,0,0,80,32);
  controlP5.addButton("Save",0,80,0,80,32);
  eliminar=controlP5.addButton("Delete",0,160,0,80,32);
  modificar=controlP5.addButton("Move",0,240,0,80,32);
  controlP5.addButton("Undelete",0,320,0,80,32);
  //controlP5.setColorBackground(0x022a79);
  //controlP5.setColorForeground(0x022a79);
  eliminar.setSwitch(true);
  modificar.setSwitch(true);
  
  
  addMouseWheelListener(new java.awt.event.MouseWheelListener() { 
    public void mouseWheelMoved(java.awt.event.MouseWheelEvent evt) { 
    if(!creando){
      float zm=1+evt.getWheelRotation()/10.0;
      println(evt);
      println("zoom: "+(zm));
      
      
      
      
      
      
      imgAncho2*=(zm);
      imgAlto2*=(zm);
      zoom=imgAncho2/imgAncho;
      zoomy=imgAlto2/imgAlto;
      println(zoom);
   }
    
    println(imgAlto+" "+imgAncho); 
  }}); 



}

 


void keyPressed(){
 if(key=='d'){
  elim=true;
  helper.setText("DELETE MODE: Click any label or press DELETE key to remove it");
 }
 if(key=='m'){
  modif=true;
  helper.setText("MOVE MODE: use UP,DOWN,LEFT and RIGHT to move the red label");
 }
if(key==CODED){
  println("entro");
if(keyCode==UP){
 Etiqueta e2 = car.getEtiquetaModif();
 if(e2!=null)
 e2.mover(0,-1);
}
if(keyCode==DOWN){
 Etiqueta e2 = car.getEtiquetaModif();
 if(e2!=null)
 e2.mover(0,1);
}
if(keyCode==LEFT){
 Etiqueta e2 = car.getEtiquetaModif();
 if(e2!=null)
 e2.mover(-1,0);
}
if(keyCode==RIGHT){
 Etiqueta e2 = car.getEtiquetaModif();
 if(e2!=null)
 e2.mover(1,0);
}
}
if(key==(DELETE)){
 boolean etBol;
  if(cargado && elim ){
    etBol = car.borrarEtiqueta(slider.getNumImagenActual());}
    else{
      etBol=false;
    } 
}
}
void keyReleased(){
 if(key=='d'){
  elim=false;
  helper.setText(MSGIDLE);
 } 
 if(key=='m'){
  modif = false;
  helper.setText(MSGIDLE);
 }
}

void Undelete(){
 car.deshacer(slider.getNumImagenActual()); 
}

public void draw() {
  stroke(255);
  background(b);
  
  

  if(cargado) {
    //image(fondoEliminar,30,220,150,140);
    PImage img = slider.getImagenActual();
    if(imgAncho2==0){
    if(img.width > width -explorador.getAncho()){
    this.imgAncho2=width/2;
    this.imgAlto2=img.height*(width/2) / img.width;}
    else{
      this.imgAlto2= height/2;
      this.imgAncho2 = img.width*(height/2) / img.height;
    }
    
  this.imgAncho=imgAncho2;
this.imgAlto=imgAlto2;}
    
    //image(marco,(width/2)-imgAncho/2 -10,(height/2)-imgAlto/2 -10,imgAncho+20,imgAlto+20);
    image(slider.getImagenActual(),(width-imgAncho2)/2 + dragX,(height-imgAlto2)/2 +dragY,imgAncho2,imgAlto2);
    if(elim ==true ||modif ==true){
      
    car.dibujaretiquetas(slider.getNumImagenActual(),(int)imgAncho2,(int)imgAlto2,true,zoom,zoomy,dragX,dragY);}
    else{
      car.dibujaretiquetas(slider.getNumImagenActual(),(int)imgAncho2,(int)imgAlto2,false,zoom,zoomy,dragX,dragY);
    }
    
    if(cargado && modif){
     car.setEtMin(); 
     
    }

if(herActual!=null){
    
    herActual.displayEstado((int)imgAncho2,(int)imgAlto2,zoom,zoomy,dragX,dragY);
  }
   //image(opciones,100.0-10,height/2.0 -42,100.0,116.0); 
  slider.display();
  explorador.display();
  
  
  helper.display();
  
  }
  
  if(!cargado){
    textFont(font,28);
    fill(255);
    String s = "Click LOAD and select a folder with images";
    
   text(s,width/2-textWidth(s)/2,height/2);
  }
  
}
/*public void mousePressed(){
 car.imagenSiguiente();
 }*/



public void Load(int valor) {
  if(!creando){
    herActual=null;
  String folderPath = selectFolder();  // Opens file chooser
  if (folderPath == null) {
    // If a folder was not selected
    println("No folder was selected...");
  } 
  else {
    // If a folder was selected, print path to folder
    println("ruta2"+folderPath);


    car = new Cargador(folderPath+"\\");
    car.cargar(this,explorador);
    slider.cargar(car.getImagenes());
    cargado=true;
    crearMenus();
    slider.setNumImagenActual(0);
    slider.actualizar();
    
  }
  }
}
public void mouseReleased(){
 slider.moverSlider(sliderValue); 
 
}
public void mousePressed() {
   xd=mouseX;
   yd=mouseY;
   println("mouse 1 "+mouseX+" "+mouseY);
    boolean bol;
    if(!creando){
    bol = slider.mousePressed();}
    else{
      bol=false;
    }
    
    if(bol)
    car.resetMinima();
    boolean etBol;
   if(!creando&&cargado && elim && mouseX >( (width/2)-imgAncho2/2 )&& mouseX < ((width/2)+imgAncho2/2) && mouseY > ((height/2)-imgAlto2/2 )&& mouseY < ((height/2)+imgAlto2/2)){
    etBol = car.borrarEtiqueta(slider.getNumImagenActual());}
    else{
      etBol=false;
    }
     
    
     if(!bol && !etBol && cargado && !elim && explorador.mousePressed() && !modif && !creando) {
      herActual = explorador.getHerramienta();
      println(herActual);
      if(herActual!=null)
      helper.setText(herActual.getTextoHelper2());
      
      /*if(herActual!=-null) {
       
            
        
        switch(et) {
        case 1:
          controlP5.addTextfield("Ancho",50,160,100,14).setAutoClear(false);
          controlP5.addTextfield("Alto",50,190,100,14).setAutoClear(false);
          helper.setText("Etiqueta cuadrada: Haga dos clicks , uno por vertice a definir");
          break;
        case 2:
          controlP5.addTextfield("Radio",50,160,100,14).setAutoClear(false);
          helper.setText("Etiqueta redonda: Haga dos clicks, uno para definir el centro y otro para el radio");
          break;
        case 3:
          helper.setText("Etiqueta poligono: Haga click izquiero para definir un vertice y derecho para cerrar el poligono");
          break;
        }
      }*/
      



      
    }
    else if(herActual!=null && !bol && !etBol && cargado && !elim && !modif && mouseX >( (width/2)-imgAncho2/2 +dragX )&& mouseX < ((width/2)+imgAncho2/2 +dragX) && mouseY > ((height/2)-imgAlto2/2 +dragY)&& mouseY < ((height/2)+imgAlto2/2) +dragY){
      
      boolean crtmp=herActual.mousePressed((int)imgAncho2,(int)imgAlto2,zoom,zoomy,dragX,dragY);
      
      if(!crtmp && !creando ){
      creando=true;
       
      eliminar.setSwitch(false);
      modificar.setSwitch(false);
      creando=!crtmp;
      }
      else if(crtmp ){
        
        
        creando=true;
        eliminar.setSwitch(true);
        modificar.setSwitch(true);
        elim=false;
        modif=false;
        eliminar.setOff();
        modificar.setOff();
        modificar.update();
        eliminar.update();
        
        creando =false;
        println("agregando");
        println(herActual);
       car.agregarEtiqueta(slider.getNumImagenActual(),herActual.getLastEtiqueta()); 
      }
      println (creando);
    }
    
  

  
}

public void Delete(){
 if(!creando){
  elim=!elim;

 if(elim){
   helper.setText("DELETE MODE: Click any label or press DELETE key to remove it");
   if(modificar.booleanValue()){
     modificar.setOff();
     
   }

modif=false;


}
else{
  if(herActual!=null){
  helper.setText(herActual.getTextoHelper2());}
  else{
    helper.setText(MSGIDLE);
  }
  
}

 }
}
public void Move(){
 if(!creando){
  modif=!modif;
  
 
 
  if(modif){
    helper.setText("MOVE MODE: use UP,DOWN,LEFT and RIGHT to move the red label");
  if(eliminar.booleanValue()){
    eliminar.setOff();
    
  }
  
  elim=false;
  
  }
  else{
    
  if(herActual!=null){
  helper.setText(herActual.getTextoHelper2());}
  else{
    helper.setText(MSGIDLE);
  }
}
  
 }
  
}

public void Save() {
  if(!creando)
  car.guardarEtiquetas();
}

public void Ancho(String texto) {
  try {
    resAncho=Integer.parseInt(texto);
  }
  catch(Exception e) {
    resAncho=-1;
  }
}

public void Alto(String texto) {
  try {
    resAlto=Integer.parseInt(texto);
  }
  catch(Exception e) {
    resAlto=-1;
  }
}

public void Radio(String texto) {
  try {
    resRadio=Integer.parseInt(texto);
  }
  catch(Exception e) {
    resRadio=-1;
  }
}







public void crearMenus() {

  controlP5.addSlider("sliderValue",0,car.getNImagenes(),0,0,height-10,width,10);

  
}



void mouseMoved() {
  
  
  if( explorador.mousePressed()){
   Herramienta her =explorador.getHerramienta();
  if(her!=null){
   helper.setTextVar(her.getTextoHelper1());
   helper.setVisible(true);
  }
 else{
 helper.setVisible(false);
 } 
  }
  else{
    helper.setVisible(false);
  }
}

void mouseDragged(){
  if(mouseButton == RIGHT){
 
 dragX+= (mouseX-xd )/100;
  dragY+=(mouseY-yd)/100;}
 
}

